package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.enums.DictionaryType;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.core.utils.ExcelUtil;
import com.zjuee.core.utils.ExportExcel;
import com.zjuee.base.dto.common.ledger.LedgerProduceBillDto;
import com.zjuee.base.model.common.ledger.LedgerProduceBill;
import com.zjuee.service.LedgerProduceBillService;
import com.zjuee.web.ledger.mapper.LedgerProduceBillMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service
public class LedgerProduceBillServiceImpl implements LedgerProduceBillService {


    @Resource
    private LedgerProduceBillMapper ledgerProduceBillMapper;

    @Resource
    private LoginDto loginDto;
    @Resource
    private TranslateData translateData;

    @Override
    public Long add(LedgerProduceBill type) {
        return Long.parseLong(String.valueOf(ledgerProduceBillMapper.insert(type)));
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        LedgerProduceBill ledgerProduceBill = ledgerProduceBillMapper.selectById(id);
        if (!validateId.equals(ledgerProduceBill.getBciId())) {
            throw new ServiceException("无权限完成操作!");
        }
        int deleteById = ledgerProduceBillMapper.deleteById(id);
        if (deleteById <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean update(LedgerProduceBill type) {
        return false;
    }

    @Override
    public LedgerProduceBill selectById(Long id, Long validateId) {
        LedgerProduceBill ledgerProduceBill = ledgerProduceBillMapper.selectById(id);
        if (!validateId.equals(ledgerProduceBill.getBciId())) {
            throw new ServiceException("无权限完成操作!");
        }
        return ledgerProduceBillMapper.selectById(id);
    }

    @Override
    public Page<LedgerProduceBill> list(LedgerProduceBillDto dto) {
        Page<LedgerProduceBill> ledgerProduceBillPage = ledgerProduceBillMapper.findByCustomPager(dto.getPage().convert(), dto);
        translateData.translateData(ledgerProduceBillPage.getRecords(),
                "LedgerProduceBillListHandle",
                String.valueOf(DictionaryType.WL_FROM.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_TYPE.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue())
        );
        return ledgerProduceBillPage;
    }

    @Override
    public void billExcel(LedgerProduceBill ledgerProduceBill) {
        List<LedgerProduceBill> ledgerProduceBills = ledgerProduceBillMapper.selectList(ledgerProduceBill);
        List<Map<String, Object>> dataMap=(List<Map<String, Object>>)translateData.translateData(ledgerProduceBills,
                "LedgerProduceBillListHandle",
                String.valueOf(DictionaryType.WL_FROM.getValue()),
//                String.valueOf(DictionaryType.WL_DEVICE_FROM.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_TYPE.getValue()),
                String.valueOf(DictionaryType.PB_WASTE_MODEL.getValue())
        );
        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        String[] key = {"id","wlpbModelStr", "wlpbTypeStr","wlpbBcode","wlpbScode", "wlpbName", "wlpbFromStr","wldbEndFromStr","wlpbStock","wlpgCreateTime"};
        String[] str = {"序号", "废物大类别","废物小类别","废物大类","废物小类","废物名称","台账来源","台账终端来源","当前库存量(吨)","创建时间"};

        List<ExportExcel> exportExcelList = new ArrayList<ExportExcel>();
        ExportExcel e1 = new ExportExcel("产生台账明细列表", "产生台账明细列表", key, str, dataMap);
        exportExcelList.add(e1);
        ExcelUtil.download( "产生台账明细列表", exportExcelList, request,response);
    }

    @Override
    public List<LedgerProduceBill> insetBatch(ArrayList<LedgerProduceBill> insertLedgerBill) {
        ledgerProduceBillMapper.insertBatch(insertLedgerBill);
        return insertLedgerBill;
    }

    @Override
    public LambdaQueryWrapper<LedgerProduceBill> fuzzyQuery(LedgerProduceBillDto dto) {
        return null;
    }
}
